package dao.hibernateImpl.moneymanage;

import java.util.List;

import model.basic.BASICCATEGORY;

import model.basic.BASICFILEBRIDGE;
import model.basic.BASICINFOBRIDGE;
import model.basic.BASICINFOMODEL;
import model.basic.BASICTOINDEXBRIDGE;
import model.basic.CLICK;
import model.basic.FILE;
import model.basic.INFOMATION;
import model.basic.TARGETINTERFACE;
import model.basic.TOINDEX;
import model.basic.UUUADMIN;


import model.moneyManage.basic.MONEYMANAGE;
import model.moneyManage.basic.SELF;
import model.moneyManage.file.MONEYFILEBRIDGE;
import model.moneyManage.info.MONEYINFOBRIDGE;
import model.moneyManage.toIndex.MONEYTOINDEXBRIDGE;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import dao.CategoryDAO;

import dao.MONEYMANAGEDAO;

import dao.helper.HibernateConfigurationHelper;
import dao.hibernateImpl.IndexHibernateImpl;


public class MONEYMANAGEHibernateImpl extends IndexHibernateImpl implements MONEYMANAGEDAO{

	private CategoryDAO CategoryDAO;
	
	

	MONEYMANAGE money=new MONEYMANAGE();
	INFOMATION info=new INFOMATION();
	CLICK click=new CLICK();
	MONEYINFOBRIDGE mib=new MONEYINFOBRIDGE();
	MONEYTOINDEXBRIDGE mtib=new MONEYTOINDEXBRIDGE();
	TOINDEX toindex=new TOINDEX();
	UUUADMIN uuadmin=new UUUADMIN();
	BASICINFOMODEL bm=((BASICINFOMODEL)money);
	MONEYFILEBRIDGE mfb=new MONEYFILEBRIDGE();
	FILE file=new FILE();

	dao.helper.HibernateConfigurationHelper hh=new HibernateConfigurationHelper();
	
	public CategoryDAO getCategoryDAO() {
		return CategoryDAO;
	}
	public void setCategoryDAO(CategoryDAO categoryDAO) {
		CategoryDAO = categoryDAO;
	}
	public List findTop4CategoryList(){
		
		BASICCATEGORY bc=new model.greenLife.category.GREENLIFECATEGORY();
		
		return CategoryDAO.findTop4CategoryList(bc);
	}



	
	public StringBuffer buildToIndexColumnSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(hh.getTableName(money.getClass()));
		sb.append(this._point);
		sb.append(money.get_ID());
		sb.append(this._comma);
		sb.append(info.get_TITLE());
		sb.append(this._comma);
		sb.append(uuadmin.get_MEMBERNAME());
		sb.append(this._comma);
		sb.append(info.get_PUBLISHER());
		return sb;
	}
	
	public StringBuffer buildToTopColumnSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(hh.getTableName(money.getClass()));
		sb.append(this._point);
		sb.append(money.get_ID());
		sb.append(this._comma);
		sb.append(info.get_TITLE());
		sb.append(this._comma);
		sb.append(uuadmin.get_MEMBERNAME());
		sb.append(this._comma);
		sb.append(info.get_DESC());
		sb.append(this._comma);
		sb.append(file.get_FILEADDR());
		return sb;
	}

	public StringBuffer buildOrderByPubTimeDesc(){
		StringBuffer sb=new StringBuffer();
		sb.append(this._orderby);
		sb.append(this._space);
		sb.append(info.get_PUBTIME());
		return sb;
	}
	public StringBuffer buildToIndexTableSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(selfTable());
		sb.append(buildInfoRalationsSql());
		sb.append(buildToIndexRalationsSql());//
		sb.append(buildToUuAdminRalationsSql());
		return sb;
	}
	public StringBuffer buildToTopTableSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(selfTable());
		sb.append(buildInfoRalationsSql());
		sb.append(buildToIndexRalationsSql());//
		sb.append(buildFileRalationsSql());//
		sb.append(buildToUuAdminRalationsSql());
		return sb;
	}
	public StringBuffer buildWhereByToIndex(){
		StringBuffer sb=new StringBuffer();
		sb=buildWhereByCondi(toindex.get_UPTOINDEX(),0);
		return sb;	
	}
	public StringBuffer buildWhereByToTop(){
		StringBuffer sb=new StringBuffer();
		sb=buildWhereByCondi(toindex.get_UPTOINDEX(),0);
		return sb;	
	}
	public StringBuffer buildWhereByCondi(String condiname,int CATEID){
		
		StringBuffer sb=new StringBuffer();
		sb.append(this.where);
		sb.append(this._space);
		sb.append(condiname);
		sb.append(this._eq);
		sb.append(2);
		return sb;	
	}
	public StringBuffer selfTable(){
		StringBuffer sb=new StringBuffer();
		String selftb=hh.getTableName(money.getClass());
		sb.append(selftb);
		
		
		return sb;
	}

	public StringBuffer buildInfoRalationsSql(){
		
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICINFOBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(mib.getClass());
		String selftb=hh.getTableName(money.getClass());
		String targettb=hh.getTableName(info.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		
		
		return sb;
		
	}
	public StringBuffer buildToIndexRalationsSql(){
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICTOINDEXBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(mtib.getClass());
		String selftb=hh.getTableName(money.getClass());
		String targettb=hh.getTableName(toindex.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		return sb;
		
	}

	public StringBuffer buildFileRalationsSql(){
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICFILEBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(mfb.getClass());
		String selftb=hh.getTableName(money.getClass());
		String targettb=hh.getTableName(file.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		return sb;
		
	}


	public StringBuffer buildToUuAdminRalationsSql(){
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICTOINDEXBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb="";
		String selftb=hh.getTableName(info.getClass());
		String targettb=hh.getTableName(uuadmin.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,info.get_PUBLISHER());
		
		return sb;
		
	}
	public List findTop1To10() {
		String sql="";
		
		sql=buildBasicSql(this.buildToIndexColumnSql().toString(), this.buildToIndexTableSql().toString(), buildWhereByToIndex().toString(), buildOrderByPubTimeDesc().toString()).toString();
		this.setSql(sql);
		
		return this.findList(0, 10);
	}

	public List findTop1() {

		String sql="";
		
		sql=buildBasicSql(this.buildToTopColumnSql().toString(), this.buildToTopTableSql().toString(), buildWhereByToTop().toString(), buildOrderByPubTimeDesc().toString()).toString();
		this.setSql(sql);
		
		return this.findList(0, 1);
	}

	public List findTop3ToIndex() {
		String sql="";

		sql=buildBasicSql(this.buildToIndexColumnSql().toString(), this.buildToIndexTableSql().toString(), buildWhereByToIndex().toString(), buildOrderByPubTimeDesc().toString()).toString();
		this.setSql(sql);
		
		return this.findList(0, 3);
	}
	
	
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		MONEYMANAGEDAO ch=(MONEYMANAGEDAO)ac.getBean("MONEYMANAGEDAO");
//		BASICCATEGORY bc=new model.video.category.VIDEOCATEGORY();
		System.out.println(((Object [])(ch.findTop3ToIndex().get(0)))[0]);
//		StringBuilder sb=ch.buildToIndexRalationsSql(new StringBuilder());
//		System.out.println(sb.toString());
	}
}
